package com.example.testrxjava.fragment.code;

import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.example.testrxjava.R;
import com.example.testrxjava.activity.search.SearchActivity;
import com.example.testrxjava.adapter.TabAdapter;
import com.example.testrxjava.base.BaseFragment;
import com.example.testrxjava.util.Action;
import com.example.testrxjava.util.CommUtil;
import com.flyco.tablayout.SlidingTabLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 项目名称：TestRxjava
 *
 * @Author 郭迎辉
 * Create by 10:20 on 2019/4/18.
 * Describe:公众号模块
 */
public class WxArticleFragment extends BaseFragment {
    @BindView(R.id.searchTv)
    EditText mSearchTv;

    @BindView(R.id.searchIv)
    ImageView mSearchIv;
    @BindView(R.id.wxTabLayout)
    SlidingTabLayout wxTabLayout;
    @BindView(R.id.wxPager)
    ViewPager wxPager;
    private List<WxArticleBean.DataBean> data;
//    WxArticleBean wxArticleBean;
    private List<Fragment> mFragmentList;
    private List<String > mSecondSystemNameList;
    private TabAdapter mTabAdapter;

    @Override
    protected void initData() {
        Action.getInstance().getWxArticleList(new WxArticleSucessCallback() {
            @Override
            public void wxArticleSucess(WxArticleBean bean) {
                for (int i = 0; i < bean.getData().size(); i++) {
                    Log.e("公众号==",bean.getData().get(i).getName());
                }
                data.addAll(bean.getData());
//                wxArticleBean=bean;
                addFragment();
            }

            @Override
            public void wxArticleFail(String message) {
                CommUtil.Toast(getActivity(),"加载失败");
            }
        });
        mSearchTv.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View view) {
                toSearchActivity();
            }
        });

    }
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void toSearchActivity() {
        Intent intent = new Intent(getActivity(), SearchActivity.class);

//        ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(MyApplication.getInstance(),
//                Pair.create(mSearchTv, getString(R.string.share_edit)),
//                Pair.create(mSearchIv, getString(R.string.share_image))
//        );
        ActivityOptionsCompat options= ActivityOptionsCompat .makeCustomAnimation(getActivity(),R.anim.anim_animo_alph_open,R.anim.anim_animo_alph_close);
        startActivity(intent, options.toBundle());
    }
    private void addFragment(){
        mFragmentList = new ArrayList<>();
        mSecondSystemNameList=new ArrayList<String>();
        for (int i = 0; i < data.size(); i++) {
            mFragmentList.add(WxArticleSFragment.getInstance(data.get(i).getId()));
            Log.e("======！！",data.get(i).getId()+"");
            mSecondSystemNameList.add(data.get(i).getName());
        }
        mTabAdapter = new TabAdapter(getActivity().getSupportFragmentManager(), mFragmentList, mSecondSystemNameList);
        wxPager.setAdapter(mTabAdapter);
        wxTabLayout.setViewPager(wxPager);
    }


    @Override
    protected View initView() {
        View view= LayoutInflater.from(getActivity()).inflate(R.layout.code_fragment,null);
        data=new ArrayList<WxArticleBean.DataBean>();
        return view;
    }
}
